Imports System.Windows.Forms

Public Class frmError

    Private _ErrorText, _ErrorDetails, _FullMsg, _LogFile As String, _Ex As Exception

    Public Sub New(ByVal ErrorText As String, ByVal Ex As Exception)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        If Me.DesignMode Then Exit Sub

        ' Add any initialization after the InitializeComponent() call.
        With System.Reflection.Assembly.GetExecutingAssembly.GetName.Version
            lblHeading.Text = String.Format("An error has occurred in WRDB at {0:g}", Now)
            lblVersion.Text = String.Format("Assembly: {1}; Version: {0}", .ToString(4), System.Reflection.Assembly.GetExecutingAssembly.GetName.Name)
        End With
        _ErrorText = ErrorText
        _Ex = Ex
        _LogFile = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\Error.log"
    End Sub

    Private Sub frmError_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Me.DesignMode Then Exit Sub

        Dim dblline As String = "".PadRight(50, "=")
        Dim line As String = "".PadRight(50, "~")
        txtErrorMsg.Text = _ErrorText.Replace("\n", vbCrLf).Replace("\t", vbTab)
        _FullMsg = String.Format("{0}\n\n{1}:\n\n{2}\n\n{3}\n\n", dblline, lblHeading.Text, lblVersion.Text, _ErrorText).Replace("\n", vbCrLf).Replace("\t", vbTab)

        If _Ex Is Nothing Then
            lnkDetail.Visible = False
        Else
            txtErrorMsg.Text &= IIf(txtErrorMsg.Text = "", "", " ") & _Ex.Message.Replace(vbCrLf, vbCr).Replace(vbCr, vbCrLf).Replace("\n", vbCrLf)
            With System.Reflection.Assembly.GetExecutingAssembly
                _FullMsg &= String.Format("{0}\n\nInformation about the executing assembly:\n\nFull Name: {1}\nVersion: {2}\nLast Updated: {3:g}", line, .Location, .GetName.Version.ToString, IO.File.GetLastWriteTime(.Location)).Replace("\n", vbCrLf)
            End With
            _ErrorDetails = String.Format("\n\n{0}\n\nThe detailed error message was:\n\n{1}\n\n", dblline, _Ex.ToString).Replace("\n", vbCrLf)
            Do While _Ex.InnerException IsNot Nothing
                _Ex = _Ex.InnerException
                _ErrorDetails &= String.Format("{0}\n\nThe next level inner exception was:\n\n{1}\n\n", line, _Ex.ToString).Replace("\n", vbCrLf)
            Loop
#If Project = "WRDBLIB" Then
            'WRDB.CloseSplashScreen()
            If Project IsNot Nothing AndAlso Project.DB IsNot Nothing Then
                With Project.DB
                    _ErrorDetails &= String.Format("{0}\n\nProject details:\n\nProject Name: {1}\nDatabase Type: {2}\nData Source: {3}\nDatabase Name: {4}", line, Project.Name, .DatabaseType.ToString, .DataSource, .DatabaseName).Replace("\n", vbCrLf)
                End With
            End If
#End If
        End If

        txtErrorMsg.Text &= _ErrorDetails
        _FullMsg &= _ErrorDetails

        Dim sw As New IO.StreamWriter(_LogFile, True)
        sw.WriteLine(_FullMsg)
        sw.Close()
        sw.Dispose()

        btnClose.Select()
    End Sub

    Private Sub lnkCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCopy.Click
        Try
            Clipboard.SetDataObject(_FullMsg, False)
        Catch ex2 As Exception
            MessageBox.Show("Unable to copy to clipboard.", "WRDB Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub lnkDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDetail.Click
        MessageBox.Show(_ErrorDetails, "WRDB Error Details", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Private Sub lnkLogFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLogFile.Click
        Process.Start(_LogFile)
    End Sub
End Class